package com.mmf.ancientcostume.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.mmf.ancientcostume.MyApplication;
import com.mmf.ancientcostume.R;
import com.mmf.ancientcostume.adapter.goods.CollectListAdapter;
import com.mmf.ancientcostume.base.activity.BaseTitleActivity;
import com.mmf.ancientcostume.base.adapter.MyBaseRecyclerAdapter;
import com.mmf.ancientcostume.common.utils.DipUtil;
import com.mmf.ancientcostume.model.GoodsDetail;
import com.mmf.ancientcostume.model.StaticData;
import com.mmf.ancientcostume.presenter.imp.goods.CollectListPresenterImp;
import com.mmf.ancientcostume.view.BaseView;
import com.mmf.ancientcostume.widget.GridSpacingItemDecoration;
import com.mmf.ancientcostume.widget.LoadRecyclerView;

import java.util.List;

import butterknife.BindView;

/**
 * Created by MMF on 2017-11-01.
 */

public class CollectListActivity extends BaseTitleActivity<CollectListPresenterImp, CollectListActivity> implements BaseView<List<GoodsDetail>>, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.rv_goods_list)
    LoadRecyclerView rvCollectList;
    @BindView(R.id.srl_refresh)
    SwipeRefreshLayout srlRefresh;
    private CollectListAdapter adapter;
    private int pageNo = 1;

    @Override
    public int getLayout() {
        return R.layout.activity_goods_list;
    }

    @Override
    public void init() {
        initAdapter();
    }

    /**
     * 初始化列表适配器
     */
    private void initAdapter() {
        rvCollectList.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        rvCollectList.addItemDecoration(new GridSpacingItemDecoration(DipUtil.dip2px(this, 2)));
        adapter = new CollectListAdapter(this);
        rvCollectList.setAdapter(adapter);
        adapter.setOnItemClickListener(new MyBaseRecyclerAdapter.OnItemClickListener<GoodsDetail>() {
            @Override
            public void onItemClick(View view, GoodsDetail item) {
                Intent intent = new Intent(CollectListActivity.this, GoodsDetailActivity.class);
                intent.putExtra("id", item.getId());
                startActivity(intent);
            }
        });
        srlRefresh.setOnRefreshListener(this);
        //判断是否滑动到最后一条记录，是就加载数据
        rvCollectList.setLoadingDataListener(new LoadRecyclerView.LoadingDataListener() {
            @Override
            public void onLoadMore() {
                pageNo++;
                getData();
            }
        });

    }

    @Override
    protected CollectListPresenterImp getPresenter() {
        return new CollectListPresenterImp();
    }

    @Override
    protected void getData() {
        presenter.getCollectList(pageNo, StaticData.PAGE_SIZE, MyApplication.getUserId());
    }

    @Override
    public void onSuccess(List<GoodsDetail> object) {
        srlRefresh.setRefreshing(false);
        adapter.addItems(object);
    }

    @Override
    public void onError(String object) {
        srlRefresh.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        srlRefresh.setRefreshing(true);
        getData();
    }
}
